package com.bijiawang.user.scheduletask;

import com.bijiawang.user.domin.Favorites;
import com.bijiawang.user.domin.Nfavorites;
import com.bijiawang.user.repository.FavoritesRepository;
import com.bijiawang.user.repository.NfavoritesRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class FavoritesSchedule {

    @Autowired
    private NfavoritesRepository nfavoritesRepository;

    @Autowired
    private FavoritesRepository favoritesRepository;
    @Autowired
    RestTemplate template;

    @Scheduled(cron = "0 0 1 * * ?")
    public void ListeningPrice() {
        List<Favorites> all = favoritesRepository.findAll();
        for (Favorites a : all) {
            String shopName1 = a.getShopName();
            if ("淘宝".equals(shopName1)) {
                String price = template.getForObject("http://PROVIDER-SEARCH/TaoBao?key={1}", String.class, a.getGoodsName());
                double v = a.getGoodsPrice();
                if (price != null) {
                    v = Double.parseDouble(price);
                }
                if (!a.getGoodsPrice().equals(v)) {
                    Nfavorites two = new Nfavorites();
                    two.setGoodsId(a.getGoodsId());
                    two.setUserName(a.getUserName());
                    two.setGoodsName(a.getGoodsName());
                    two.setGoodsPrice(v);
                    two.setGoodsUrl(a.getGoodsUrl());
                    two.setShopName(a.getShopName());
                    nfavoritesRepository.saveAndFlush(two);
                }
            } else if ("京东".equals(shopName1)) {
                String price = template.getForObject("http://PROVIDER-SEARCH/JD?key={1}", String.class, a.getGoodsName());
                double v = a.getGoodsPrice();
                if (price != null) {
                    v = Double.parseDouble(price);
                }
                if (!a.getGoodsPrice().equals(v)) {
                    Nfavorites two = new Nfavorites();
                    two.setGoodsId(a.getGoodsId());
                    two.setUserName(a.getUserName());
                    two.setGoodsName(a.getGoodsName());
                    two.setGoodsPrice(v);
                    two.setGoodsUrl(a.getGoodsUrl());
                    two.setShopName(a.getShopName());
                    nfavoritesRepository.saveAndFlush(two);
                }
            } else if ("天猫".equals(shopName1)) {
                String price = template.getForObject("http://PROVIDER-SEARCH/Tmall?key={1}", String.class, a.getGoodsName());
                double v = a.getGoodsPrice();
                if (price != null) {
                    v = Double.parseDouble(price);
                }
                if (!a.getGoodsPrice().equals(v)) {
                    Nfavorites two = new Nfavorites();
                    two.setGoodsId(a.getGoodsId());
                    two.setUserName(a.getUserName());
                    two.setGoodsName(a.getGoodsName());
                    two.setGoodsPrice(v);
                    two.setGoodsUrl(a.getGoodsUrl());
                    two.setShopName(a.getShopName());
                    nfavoritesRepository.saveAndFlush(two);
                }
            } else if ("苏宁易购".equals(shopName1)) {
                String price = template.getForObject("http://PROVIDER-SEARCH/SuNing?key={1}", String.class, a.getGoodsName());
                double v = a.getGoodsPrice();
                if (price != null) {
                    v = Double.parseDouble(price);
                }
                if (!a.getGoodsPrice().equals(v)) {
                    Nfavorites two = new Nfavorites();
                    two.setGoodsId(a.getGoodsId());
                    two.setUserName(a.getUserName());
                    two.setGoodsName(a.getGoodsName());
                    two.setGoodsPrice(v);
                    two.setGoodsUrl(a.getGoodsUrl());
                    two.setShopName(a.getShopName());
                    nfavoritesRepository.saveAndFlush(two);
                }
            } else if ("当当".equals(shopName1)) {
                String price = template.getForObject("http://PROVIDER-SEARCH/DD?url={1}", String.class, a.getGoodsUrl());
                double v = a.getGoodsPrice();
                if (price != null) {
                    v = Double.parseDouble(price);
                }
                if (!a.getGoodsPrice().equals(v)) {
                    Nfavorites two = new Nfavorites();
                    two.setGoodsId(a.getGoodsId());
                    two.setUserName(a.getUserName());
                    two.setGoodsName(a.getGoodsName());
                    two.setGoodsPrice(v);
                    two.setGoodsUrl(a.getGoodsUrl());
                    two.setShopName(a.getShopName());
                    nfavoritesRepository.saveAndFlush(two);
                }
            }
        }
    }
}
